<template>
	<view class="accountSwitch-wrap">
		<view class="accountSwitch-list">
			<view class="accountSwitch-item" @click="pageTypeChange(0)">
				<view class="accountSwitch-item-txt">患者端</view>
				<image v-if="pageType != 0" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/address_gray@2x.png"></image>
				<image v-if="pageType == 0" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/pay_success@2x.png"></image>
			</view>

			<view class="accountSwitch-item" @click="pageTypeChange(1)">
				<view class="accountSwitch-item-txt">医生端</view>
				<image v-if="pageType != 1" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/address_gray@2x.png"></image>
				<image v-if="pageType == 1" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/pay_success@2x.png"></image>
			</view>

			<view class="accountSwitch-item" @click="pageTypeChange(2)">
				<view class="accountSwitch-item-txt">药师端</view>
				<image v-if="pageType != 2" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/address_gray@2x.png"></image>
				<image v-if="pageType == 2" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/pay_success@2x.png"></image>
			</view>
			<view class="accountSwitch-item" @click="pageTypeChange(3)">
				<view class="accountSwitch-item-txt">机构端</view>
				<image v-if="pageType != 3" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/address_gray@2x.png"></image>
				<image v-if="pageType == 3" class="accountSwitch-item-icon" src="http://k8s.vendingtech.vip/images/pay_success@2x.png"></image>
			</view>
		</view>

		<u-bottom-button value="确认切换" @click="identitySwitch"></u-bottom-button>
	</view>
</template>
<script>
import uBottomButton from '@/components/u-bottom-button/u-bottom-button.vue';
import { imInfo, doctorInfo, currentImInfo } from '@/utils/token.js';
const app = getApp();
export default {
	components: {
		uBottomButton
	},
	data() {
		return {
			pageType: 0, //0 患者  1医生  2药师  3机构
			isDoctorAuth: false, //医生认证
			isPharmacistAuth: false //药师认证
		};
	},
	onLoad(option) {
		this.pageType = option.pageType ? option.pageType : 0;
	},
	onShow() {},
	methods: {
		pageTypeChange(type) {
			this.pageType = type;
		},

		//确认切换
		/*
		confirmSwitch(token, accid) {
			if (this.pageType == 0) {
				app.globalData.role = 0;
				app.globalData.nim.destroy({
					done: () => {
						this.initIm(imInfo.get().token, imInfo.get().accid);
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}
				});
			} else if (this.pageType == 1) {
				app.globalData.role = 1;
				app.globalData.nim.destroy({
					done: () => {
						this.initIm(token, accid);
						uni.reLaunch({
							url: '/pages/doctor/doctor'
						});
					}
				});
			} else {
				app.globalData.role = 2;

				app.globalData.nim.destroy({
					done: () => {
						this.initIm(token, accid);
						uni.reLaunch({
							url: '/pages-doctor/pharmacist/pharmacist'
						});
					}
				});
			}
		},
		*/
	   /*
	    confirmSwitch(sdkAppId, id, userSig) {
			if (this.pageType == 0) {
				app.globalData.role = 0;
				app.globalData.tim.destroy().then(() => {
					currentImInfo.set({
						sdkAppId: imInfo.get().sdkAppId,
						id: imInfo.get().id,
						userSig: imInfo.get().userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					this.initTxyIm(imInfo.get().sdkAppId, imInfo.get().id, imInfo.get().userSig);
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}).catch(() => {
					currentImInfo.set({
						sdkAppId: imInfo.get().sdkAppId,
						id: imInfo.get().id,
						userSig: imInfo.get().userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					this.initTxyIm(imInfo.get().sdkAppId, imInfo.get().id, imInfo.get().userSig);
					uni.reLaunch({
						url: '/pages/index/index'
					});
				})
			} else if (this.pageType == 1) {
				app.globalData.role = 1;
				app.globalData.tim.destroy().then(() => {
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					this.initTxyIm(sdkAppId, id, userSig);
					uni.reLaunch({
						url: '/pages/doctor/doctor'
					});
				}).catch(() => {
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					this.initTxyIm(sdkAppId, id, userSig);
					uni.reLaunch({
						url: '/pages/doctor/doctor'
					});
				})
			} else {
				app.globalData.role = 2;
				app.globalData.tim.destroy().then(() => {
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					this.initTxyIm(sdkAppId, id, userSig);
					uni.reLaunch({
						url: '/pages-doctor/pharmacist/pharmacist'
					});
				}).catch(() => {
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					this.initTxyIm(sdkAppId, id, userSig);
					uni.reLaunch({
						url: '/pages-doctor/pharmacist/pharmacist'
					});
				})
			}
	    },
		*/
		confirmSwitch(sdkAppId, id, userSig) {
			if (this.pageType == 0) {
				app.globalData.role = 0;
				currentImInfo.set({
					sdkAppId: imInfo.get().sdkAppId,
					id: imInfo.get().id,
					userSig: imInfo.get().userSig
				}, Date.parse(new Date()) + (86400000 * 29));
				getApp().globalData.txyImController.logout();
				getApp().globalData.txyImController.login(imInfo.get().id, imInfo.get().userSig);
				uni.reLaunch({
					url: '/pages/index/index'
				});
			} else if (this.pageType == 1) {
				app.globalData.role = 1;
				currentImInfo.set({
					sdkAppId,
					id,
					userSig
				}, Date.parse(new Date()) + (86400000 * 29));
				getApp().globalData.txyImController.logout();
				getApp().globalData.txyImController.login(id, userSig);
				uni.reLaunch({
					url: '/pages/doctor/doctor'
				});
			} else if (this.pageType == 2) {
				app.globalData.role = 2;
				currentImInfo.set({
					sdkAppId,
					id,
					userSig
				}, Date.parse(new Date()) + (86400000 * 29));
				getApp().globalData.txyImController.logout();
				getApp().globalData.txyImController.login(id, userSig);
				uni.reLaunch({
					url: '/pages-doctor/pharmacist/pharmacist'
				});
			} else {
				app.globalData.role = 3;
				// currentImInfo.set({
				// 	sdkAppId,
				// 	id,
				// 	userSig
				// }, Date.parse(new Date()) + (86400000 * 29));
				// getApp().globalData.txyImController.logout();
				// getApp().globalData.txyImController.login(id, userSig);
				uni.reLaunch({
					url: '/pages-agencies/agencies/agencies'
				});
			}
		},

		//获取医生/药师个人信息
		getDoctorInfo() {
			this.$api.getDoctorInfo({}).then(res => {
				if (this.pageType != 0 && res.code == 200 && res.result != null) {
					// this.confirmSwitch(res.result?.imToken, res.result?.id);
					doctorInfo.set(res.result, Date.parse(new Date()) + (86400000 * 29))
					// 获取医生/药师的腾讯云IM信息
					this.$api.doctorTxyImInfo({
						params: {
							id: res.result.id
						}
					}).then(res_ => {
						this.confirmSwitch(res_.result.sdkAppId, res_.result.id, res_.result.userSig);	
					})
				} else if (this.pageType == 0) {
					this.confirmSwitch();
				} else {
					uni.showToast({
						title: '身份切换失败',
						icon: 'none'
					});
				}
			});
		},
		
		getAgencyInfo() {
			this.confirmSwitch(3);
		},

		//切换角色
		identitySwitch() {
			this.$api
				.identitySwitch({
					params: {
						role: this.pageType
					}
				})
				.then(res => {
					if (this.pageType == 3) {
						if (res.code == 200) {
							this.getAgencyInfo();
							// this.gotoRouter('/pages-agencies/agencies/agencies');
						} else {
							this.$api.getAgenciesState().then(e => {
								const { code, result } = e
								if (code == 200) {
									if (result.code == -1) {
										//跳转申请页面
										this.gotoRouter('/pages-agencies/settled/settled');
									}
									if(result.code == 0){
										this.gotoRouter('/pages-agencies/auditDuring/auditDuring');
									}
									if(result.code == 2){
										this.gotoRouter('/pages-agencies/auditRefused/auditRefused');
									}
								}
							})
						}
					} else {
						if (res.code == 200) {
							this.getDoctorInfo();
						} else {
							//跳转申请页面
							this.gotoRouter('/pages-doctor/auth/auth?doctorType=' + this.pageType);
						}
					}
					
				});
		}
	}
};
</script>

<style lang="less">
@import './accountSwitch.less';
</style>
